﻿@{
    ViewBag.Title = "用户管理";
}

@section SideNav{@Html.Partial("SideNavPartialView")}

<ol class="breadcrumb">
    <li><span class="glyphicon glyphicon-home"></span>  @Html.ActionLink("首页", "Index", "Home")</li>
    <li>@Html.ActionLink("用户管理", "Index", "User")</li>
    <li class="active">@Html.ActionLink("角色管理", "Index", "user")</li>
</ol>
<div id="toolbar" class="btn-group" role="group">
    <button id="btn_add" class="btn btn-default"><span class="glyphicon glyphicon-plus"></span> 添加</button>
    <button id="btn_del" class="btn btn-default"><span class="glyphicon glyphicon-remove"></span> 删除</button>
</div>
<table id="admingrid"></table>
@section style{
    @Styles.Render("~/Content/bootstrapplugincss")
}

@section scripts{
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/bootstrapplugin")
    <script type="text/javascript">
        $(document).ready(function () {
            //表格
            var $table = $('#admingrid');
            $table.bootstrapTable({
                toolbar: "#toolbar",
                striped: true,
                showRefresh: true,
                showColumns: true,
                method: "post",
                url: "@Url.Action("ListJson")",
                columns: [
                    { title: "state", checkbox: true },
                    { title: "ID", field: "UserID" },
                    { title: "用户名", field: "UserName", formatter: function (value, row, index) { return "<a href='@Url.Action("Modify", "Role")/" + row.Userid + "'>" + value + "</a>" } },
                    { title: "昵称", field: "DisplayName" },
                    { title: "状态", field: "StatusStr" },
                { title: "操作", field: "Userid", formatter: function (value) { return "<a class='btn btn-sm btn-danger' data-operation='deleterole' data-value='" + value + "'>删除</a>" } }
                ],
                onLoadSuccess: function () {
                    //删除按钮
                    $("a[data-operation='deleterole']").click(function () {
                        var id = $(this).attr("data-value");
                        BootstrapDialog.confirm("你确定要删除" + $(this).parent().parent().find("td").eq(1).text() + "吗？", function (result) {
                            if (result) {
                                $.post("@Url.Action("DeleteJson", "user")", { id: id }, function (data) {
                                    if (data.Code == 1) {
                                        BootstrapDialog.show({
                                            message: "删除角色成功",
                                            buttons: [{
                                                icon: "glyphicon glyphicon-ok",
                                                label: "确定",
                                                action: function (dialogItself) {
                                                    $table.bootstrapTable("refresh");
                                                    dialogItself.close();
                                                }
                                            }]

                                        });
                                    }
                                    else BootstrapDialog.alert(data.Message);
                                }, "json");
                            }
                        });
                    });
                    //删除按钮结束
                }
            });
            //表格结束
            //添加按钮
            $("#btn_add").click(function () {
                var addDialog = new BootstrapDialog({
                    title: "<span class='glyphicon glyphicon-plus'></span>添加用户",
                    message: function (dialog) {
                        var $message = $('<div></div>');
                        var pageToLoad = dialog.getData('pageToLoad');
                        $message.load(pageToLoad);

                        return $message;
                    },
                    data: {
                        'pageToLoad': '@Url.Action("AddPartialView")'
                    },
                    buttons: [{
                        icon: "glyphicon glyphicon-plus",
                        label: "添加",
                        action: function (dialogItself) {
                            $.post($("form").attr("action"), $("form").serializeArray(), function (data) {
                                if (data.Code == 1) {
                                    BootstrapDialog.show({
                                        message: data.Message,
                                        buttons: [{
                                            icon: "glyphicon glyphicon-ok",
                                            label: "确定",
                                            action: function (dialogItself) {
                                                $table.bootstrapTable("refresh");
                                                dialogItself.close();
                                                addDialog.close();
                                            }
                                        }]

                                    });
                                }
                                else BootstrapDialog.alert(data.Message);
                            }, "json");
                            $("form").validate();
                        }
                    }, {
                        icon: "glyphicon glyphicon-remove",
                        label: "关闭",
                        action: function (dialogItself) {
                            dialogItself.close();
                        }
                    }]
                });
                addDialog.open();
            });
            //添加按钮结束
        });
    </script>
}